﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;

namespace FLKRP.WP7.Server.Server
{
    class Listener
    {
        bool _running;
        TcpListener _listener;

        public event EventHandler<ClientConnectedEventArgs> ClientConnected;

        public Listener(IPAddress addr, int port)
        {
            _listener = new TcpListener(addr, port);
        }

        public void Start()
        {
            var listenerThread = new Thread(() =>{
                            _running = true;
                            _listener.Start();
                            while (_running)
                            {
                                try
                                {
                                    var socket = _listener.AcceptSocket();
                                    if (socket != null)
                                    {
                                        Debug.WriteLine("Socket Connected.");
                                        if (ClientConnected != null)
                                            ClientConnected(this, new ClientConnectedEventArgs(socket));
                                    }
                                }
                                catch { /* Exception thrown when listener is aborted, catch and exit while loop */ }
                            }

                            Debug.WriteLine("Leaving while loop.");
                        });

            listenerThread.Start();
        }

        public void Stop()
        {
            _running = false;
            _listener.Stop();
        }
    }
}
